package com.dearbinge.opentapi.content.collect;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.collect.ShopCollectService;
import com.dearbinge.data.api.shopManage.ShopManageService;
import com.dearbinge.data.pojo.collect.ShopCollectDO;
import com.dearbinge.data.pojo.collect.vo.ShopCollectRequestVO;
import com.dearbinge.data.pojo.collect.vo.ShopCollectResponseVO;
import com.dearbinge.data.pojo.shopManage.ShopManage;
import com.dearbinge.utils.ResultMsg;
import com.dearbinge.utils.enums.ResultStatusCodeEnum;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * 收藏控制层
 *
 * @author wes
 * @date 2019/1/10
 */
@Api(tags = "6.1、店铺-收藏")
@RestController
@RequestMapping("/shopCollect")
@CrossOrigin
@EnableAutoConfiguration
public class ShopCollectController extends WebMvcConfigurerAdapter {

    @Autowired
    private ShopCollectService shopCollectService;
    @Autowired
    private ShopManageService shopManageService;


    @ApiOperation(value = "增加修改收藏", notes = "增加修改收藏")
    @RequestMapping(value = "/addOrUpdateCollect", method = RequestMethod.POST)
    public ResultMsg<String> addOrUpdateCollect(@RequestBody ShopCollectRequestVO collect) {
        try {
            ShopCollectDO shopCollectDO = new ShopCollectDO();
            BeanUtils.copyProperties(collect,shopCollectDO);
            Integer dataCode ;
            if(collect.getShopCollectId() == null || collect.getShopCollectId() ==0 ){
                //校验收藏自己
                Integer shopId = collect.getShopId();
                ShopManage shopManage = shopManageService.getByUserId(collect.getUserId().toString());
                if (shopManage != null && shopManage.getId() .equals(shopId)) {
                    return ResultMsg.error(500, "不能收藏自己的店铺");
                }
                dataCode = shopCollectService.insertCollect(shopCollectDO);
                if (dataCode >0){
                    return ResultMsg.ok("增加成功");
                }
            }else {
                dataCode = shopCollectService.updateCollect(shopCollectDO);
                if (dataCode >0){
                    return ResultMsg.ok("修改成功");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), ResultStatusCodeEnum.CODE500.getDesc());
    }


    @ApiOperation(value = "根据ID删除", notes = "根据ID删除")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query")
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public ResultMsg<String> delete(Integer id) {
        try {
            Integer dataCode = shopCollectService.deleteCollect(id);
            if (dataCode > 0){
                return ResultMsg.ok("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }


    @ApiOperation(value = "根据id查询",notes = "根据id查询")
    @ApiImplicitParam(name = "id", value = "收藏表id", paramType = "query")
    @RequestMapping(value = "/getCollectById",method = RequestMethod.GET)
    public ResultMsg<ShopCollectResponseVO> getCollectById(Integer id) {
        try {
            ShopCollectResponseVO collect = shopCollectService.getCollectById(id);
            return ResultMsg.ok(collect);

        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }


    @ApiOperation(value = "查询用户店铺收藏", notes = "查询用户店铺收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "userId", paramType = "query", required = true),
            @ApiImplicitParam(name = "current", value = "current", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "size", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(response = ShopCollectResponseVO.class, code = 200, message = "返回结果参数")
    })
    @RequestMapping(value = "/listCollectByUserId", method = RequestMethod.GET)
    public ResultMsg<Page> listCollectByUserId(String userId, Integer current, Integer size) {
        try {
            Page page = shopCollectService.listCollectByUserId(userId, current, size);
            return ResultMsg.okData(page, ResultStatusCodeEnum.SUCCESS.getDesc());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), ResultStatusCodeEnum.CODE500.getDesc());
    }



}
